package net.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Socket Client
 * 
 * @author zhang-hc
 *
 */
public class SocketClient {

	static Socket socket;
	static final int port = 8080;
	
	public static void main(String[] args) {
		BufferedReader reader = null;
		BufferedReader in = null;
		PrintWriter writer = null;
		try {
			socket = new Socket("127.0.0.1", port);
			
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
			in = new BufferedReader(new InputStreamReader(System.in));
			String s;
			s = in.readLine();
			while(!s.equals("bye")){
				writer.println(s);
				writer.flush();
				s = in.readLine();
			}
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				in.close();
				reader.close();
				writer.close();
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
